﻿
Imports elpmis
Imports webctrl

Namespace httpFunctions

    'parameters, page, optinoal, the xhtmlpage instance
    '            inlineScript, optional, the inlineScript of an xhtmlpage, overwrite page parameter
    '            url, any, convert to string
    '            username, optional, any, convert to string
    '            password, optional, any, convert to string
    '            body, optional, any, convert to string
    '            failedToGetResponse, any, convert to string, must have '' wrapped, if not a string var
    'return, string, action link
    Public Class jsajaxresponse
        Inherits internalFunction

        Public Class parameters
            Public Const page As String = "page"
            Public Const inlineScript As String = "inlineScript"
            Public Const url As String = "url"
            Public Const username As String = "username"
            Public Const password As String = "password"
            Public Const body As String = "body"
            Public Const failedToGetResponse As String = "failedToGetResponse"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.page, parameters.url, parameters.failedToGetResponse)
        End Sub

        Public Sub New()
            MyBase.New("js.ajax.response", m)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim url As String = Nothing
            If ctx.findVariableStr(parameters.url, url) Then
                Dim inlineScript As inlineScript = Nothing
                getinlinescript(ctx, inlineScript, parameters.page, parameters.inlineScript)
                Dim username As String = Nothing
                username = ctx.findVariableStr(parameters.username)
                Dim password As String = Nothing
                password = ctx.findVariableStr(parameters.password)
                Dim body As String = Nothing
                body = ctx.findVariableStr(parameters.body)
                Dim failedToGetResponse As String = Nothing
                failedToGetResponse = ctx.findVariableStr(parameters.failedToGetResponse)

                output = New bare_variable(typeDefination.string, _
                                           ajaxresponse.invoke(inlineScript, url, username, _
                                                               password, body, failedToGetResponse))
                Return True
            Else
                raiseError("expect parameter " + parameters.url, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

    'parameters, page, optinoal, the xhtmlpage instance
    '            inlineScript, optional, the inlineScript of an xhtmlpage, overwrite page parameter
    '            url, any, convert to string
    '            username, optional, any, convert to string
    '            password, optional, any, convert to string
    '            body, optional, any, convert to string
    '            callback, any, convert to string
    '            failedToGetResponse, any, convert to string, must have '' wrapped, if not a string var
    'return, string, action link
    Public Class jsajaxcall
        Inherits internalFunction

        Public Class parameters
            Public Const page As String = "page"
            Public Const inlineScript As String = "inlineScript"
            Public Const url As String = "url"
            Public Const username As String = "username"
            Public Const password As String = "password"
            Public Const body As String = "body"
            Public Const callback As String = "callback"
            Public Const failedToGetResponse As String = "failedToGetResponse"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.page, parameters.url, parameters.callback, parameters.failedToGetResponse)
        End Sub

        Public Sub New()
            MyBase.New("js.ajax.call", m)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim url As String = Nothing
            If ctx.findVariableStr(parameters.url, url) Then
                Dim callback As String = Nothing
                If ctx.findVariableStr(parameters.callback, callback) Then
                    Dim inlineScript As inlineScript = Nothing
                    getinlinescript(ctx, inlineScript, parameters.page, parameters.inlineScript)
                    Dim username As String = Nothing
                    username = ctx.findVariableStr(parameters.username)
                    Dim password As String = Nothing
                    password = ctx.findVariableStr(parameters.password)
                    Dim body As String = Nothing
                    body = ctx.findVariableStr(parameters.body)
                    Dim failedToGetResponse As String = Nothing
                    failedToGetResponse = ctx.findVariableStr(parameters.failedToGetResponse)

                    output = New bare_variable(typeDefination.string, _
                                               ajaxcall.invoke(inlineScript, url, username, _
                                                               password, body, callback, failedToGetResponse))
                    Return True
                Else
                    raiseError("expect parameter " + parameters.callback, errorHandle.errorType.user)
                    Return False
                End If
            Else
                raiseError("expect parameter " + parameters.url, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

End Namespace